package com.cff.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.cff.bean.ViewLog;
import com.cff.common.DateUtil;
import com.cff.common.FileUtil;

public class LogDetailModelFromFile implements ILogDetailModel{

	@Override
	public List<ViewLog> getViewLog() {
		String filePath = FileUtil.getDataPath();
		String fileName = filePath + "/baby-" + DateUtil.getNowDate() + ".txt";
		List<ViewLog> viewLogsList = new ArrayList<ViewLog>();
		if (!FileUtil.isFileExists(fileName)){
			return null;
		}
		try {
			String fileContent = FileUtil.readFile(fileName);
			String viewLogs[] = fileContent.split("\\n");
			for (int i = viewLogs.length-1; i >=0; i--){
				ViewLog viewLog = new ViewLog();
				viewLog.convert(viewLogs[i]);
				viewLogsList.add(viewLog);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
			return viewLogsList;
		}
		return viewLogsList;
	}

	@Override
	public void saveViewLog(ViewLog log) {
		String filePath = FileUtil.getDataPath();
		String fileName = filePath + "/baby-" + DateUtil.getNowDate() + ".txt";
		try {
			FileUtil.appendFileContent(fileName,log.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
